home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Libraries / Effect library / Demo ƒ / MSG Shell ƒ / msg main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-01-12  |  6.6 KB  |  304 lines  |  [TEXT/KAHL]

  1. /**********************************************************************\
  2.  
  3. File:        msg main.c
  4.  
  5. Purpose:    This module handles the event loop and event dispatching.
  6.  
  7.  
  8. MSG Demo -- graphic effects demonstration program
  9. Copyright (C) 1992-4 Mark Pilgrim & Dave Blumenthal
  10.  
  11. This program is free software; you can redistribute it and/or modify
  12. it under the terms of the GNU General Public License as published by
  13. the Free Software Foundation; either version 2 of the License, or
  14. (at your option) any later version.
  15.  
  16. This program is distributed in the hope that it will be useful,
  17. but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19. GNU General Public License for more details.
  20.  
  21. You should have received a copy of the GNU General Public License
  22. along with this program in a file named "GNU General Public License".
  23. If not, write to the Free Software Foundation, 675 Mass Ave,
  24. Cambridge, MA 02139, USA.
  25.  
  26. \**********************************************************************/
  27.  
  28. #include "msg main.h"
  29. #include "msg integrity.h"
  30. #include "msg graphics.h"
  31. #include "msg about.h"
  32. #include "msg help.h"
  33. #include "msg menus.h"
  34. #include "msg sounds.h"
  35. #include "msg prefs.h"
  36. #include "msg environment.h"
  37. #include "demo.h"
  38. #include "AppleEvents.h"
  39. #include "EPPC.h"
  40.  
  41. void main(void)
  42. {
  43.     MaxApplZone();    
  44.     InitGraf(&thePort);
  45.     InitFonts();
  46.     FlushEvents(everyEvent, 0);
  47.     InitWindows();
  48.     InitMenus();
  49.     TEInit();
  50.     InitDialogs(0L);
  51.     InitCursor();
  52.     GetDateTime(&randSeed);
  53.     
  54.     CheckEnvironment();    
  55.     DoIntegrityCheck();    
  56.     InitSounds();
  57.     InitMSGGraphics();
  58.     InitHelp();
  59.     PrefsError(PreferencesInit());
  60.     InitEnvironment();    
  61.     InitProgram();
  62.     EventLoop();    
  63.     ShutDownEnvironment();
  64.     ExitToShell();
  65. }
  66.  
  67. void EventLoop(void)
  68. {
  69.     EventRecord        theEvent;
  70.     int                i;
  71.     
  72.     while (!gDone)
  73.     {
  74.         SetCursor(&arrow);
  75.         HiliteMenu(0);
  76.         
  77.         if (FrontWindow()!=0L)
  78.         {
  79.             for (i=0; i<NUM_WINDOWS; i++)
  80.             {
  81.                 if (FrontWindow()==gTheWindow[i])
  82.                 {
  83.                     SetPort(gTheWindow[i]);
  84.                     i=NUM_WINDOWS;
  85.                 }
  86.             }
  87.         }
  88.             
  89.         if (WaitNextEvent(everyEvent, &theEvent, ((gTheWindow[kAboutMSG]!=0L) &&
  90.             (FrontWindow()==gTheWindow[kAboutMSG])) ? 0 : 30, 0L))
  91.             DispatchEvents(theEvent);
  92.         
  93.         if ((!gIsInBackground) && (gTheWindow[kAboutMSG]!=0L) &&
  94.             (FrontWindow()==gTheWindow[kAboutMSG]))
  95.             DoTheMSGThing();
  96.     }
  97. }
  98.  
  99. void DispatchEvents(EventRecord theEvent)
  100. {
  101.     int                i;
  102.     OSErr            isHuman;
  103.     Point            thisPoint;
  104.     int                index;
  105.     char            thisChar;
  106.     unsigned long    dummy;
  107.     WindowPtr        theWindow;
  108.     
  109.     index=-1;
  110.     if ((theWindow=FrontWindow())!=0L)
  111.     {
  112.         for (i=0; (i<NUM_WINDOWS) && (index==-1); i++)
  113.             if (theWindow==gTheWindow[i])
  114.                 index=i;
  115.     }
  116.     
  117.     switch (theEvent.what)
  118.     {
  119.         case nullEvent:
  120.             break;
  121.         case mouseDown:
  122.             HandleMouseDown(theEvent);
  123.             break;
  124.         case keyDown:
  125.         case autoKey:
  126.             thisChar=(char)(theEvent.message & charCodeMask);
  127.             if(theEvent.modifiers & cmdKey)
  128.             {
  129.                 AdjustMenus();
  130.                 HandleMenu(MenuKey(thisChar));
  131.             }
  132.             else
  133.                 switch (index)
  134.                 {
  135.                     case kAbout:
  136.                         CloseTheWindow(index);
  137.                         break;
  138.                     case kAboutMSG:
  139.                         UpdateTheWindow(index);
  140.                         Delay(30, &dummy);
  141.                         CloseTheWindow(index);
  142.                         break;
  143.                     case kHelp:
  144.                         HelpKeyEvent(thisChar);
  145.                         break;
  146.                     default:
  147.                         ProgramKeyEvent(thisChar);
  148.                         break;
  149.                 }
  150.             break;
  151.         case diskEvt:
  152.             if (HiWord(theEvent.message)!=noErr)
  153.             {
  154.                 DILoad();
  155.                 SetPt(&thisPoint, 120, 120);
  156.                 isHuman=DIBadMount(thisPoint, theEvent.message);
  157.                 DIUnload();
  158.             }
  159.             break;
  160.         case updateEvt:
  161.             BeginUpdate((WindowPtr)theEvent.message);
  162.             
  163.             for (i=0; i<NUM_WINDOWS; i++)
  164.                 if ((WindowPtr)theEvent.message == gTheWindow[i])
  165.                     UpdateTheWindow(i);
  166.                     
  167.             EndUpdate((WindowPtr)theEvent.message);
  168.             break;
  169.         case activateEvt:
  170.             if (((WindowPtr)theEvent.message==gTheWindow[kAboutMSG]) &&
  171.                 ((theEvent.modifiers&activeFlag)==0))
  172.                 UpdateTheWindow(kAboutMSG);
  173.             break;
  174.         case osEvt:
  175.             if (((theEvent.message>>24)&0x0FF)==suspendResumeMessage)
  176.             {
  177.                 gIsInBackground=((theEvent.message&resumeFlag)==0);
  178.                 if ((gIsInBackground) && ((gTheWindow[kAboutMSG]!=0L) &&
  179.                     (FrontWindow()==gTheWindow[kAboutMSG])))
  180.                     UpdateTheWindow(kAboutMSG);
  181.             }
  182.             break;
  183.         case kHighLevelEvent:
  184.             if (gHasAppleEvents)
  185.                 AEProcessAppleEvent(&theEvent);
  186.             break;
  187.     }
  188. }
  189.  
  190. void HandleMouseDown(EventRecord theEvent)
  191. {
  192.     WindowPtr            theWindow;
  193.     int                    windowCode;
  194.     long                windSize;
  195.     GrafPtr                oldPort;
  196.     int                    i;
  197.     Rect                sizeRect;
  198.     Boolean                gotone;
  199.     int                    index;
  200.     unsigned long        dummy;
  201.     
  202.     windowCode=FindWindow(theEvent.where, &theWindow);
  203.     index=-1;
  204.     for (i=0; (i<NUM_WINDOWS) && (index==-1); i++)
  205.         if (theWindow==gTheWindow[i])
  206.             index=i;
  207.     
  208.     switch (windowCode)
  209.     {
  210.         case inMenuBar:
  211.             AdjustMenus();
  212.             HandleMenu(MenuSelect(theEvent.where));
  213.             break;
  214.         case inContent:
  215.             if(FrontWindow() != theWindow)
  216.             {
  217.                 if (FrontWindow()==gTheWindow[kAboutMSG])
  218.                     UpdateTheWindow(kAboutMSG);
  219.                 SelectWindow(theWindow);
  220.             }
  221.             else
  222.                 switch (index)
  223.                 {
  224.                     case kAbout:
  225.                         CloseTheWindow(index);
  226.                         break;
  227.                     case kAboutMSG:
  228.                         UpdateTheWindow(index);
  229.                         Delay(30, &dummy);
  230.                         CloseTheWindow(index);
  231.                         break;
  232.                     case kHelp:
  233.                         HelpEvent();
  234.                         break;
  235.                     default:
  236.                         ProgramEvent();
  237.                         break;
  238.                 }
  239.             break;
  240.         case inSysWindow:
  241.             SystemClick(&theEvent, theWindow);
  242.             break;
  243.         case inDrag:
  244.             DragWindow(theWindow, theEvent.where, &gDragRect);
  245.             gWindowBounds[index] = (*(((WindowPeek)gTheWindow[index])->contRgn))->rgnBBox;
  246.             break;
  247.         case inGoAway:
  248.             if (TrackGoAway(theWindow, theEvent.where))
  249.             {
  250.                 gotone=FALSE;
  251.                 for (i=0; (i<NUM_WINDOWS) && (!gotone); i++)
  252.                     gotone=(theWindow==gTheWindow[i]);
  253.                     
  254.                 if (gotone)
  255.                     CloseTheWindow(i-1);
  256.                 else
  257.                     DisposeWindow(theWindow);
  258.                 
  259.                 AdjustMenus();
  260.             }
  261.             break;
  262.         case inGrow:
  263.             sizeRect = screenBits.bounds;
  264.             OffsetRect(&sizeRect, sizeRect.left, sizeRect.top);
  265.             
  266.             windSize = GrowWindow(theWindow, theEvent.where, &sizeRect);
  267.             if(windSize != 0)
  268.             {
  269.                 GetPort(&oldPort);
  270.                 SetPort(theWindow);
  271.                 EraseRect(&theWindow->portRect);
  272.                 SizeWindow(theWindow, LoWord(windSize), HiWord(windSize), TRUE);
  273.                 InvalRect(&theWindow->portRect);
  274.                 SetPort(oldPort);
  275.             }
  276.             
  277.             gWindowBounds[index] = (*(((WindowPeek)gTheWindow[index])->contRgn))->rgnBBox;
  278.             
  279.             break;
  280.         case inZoomIn:
  281.         case inZoomOut:
  282.             if(TrackBox(theWindow, theEvent.where, windowCode))
  283.             {
  284.                 GetPort(&oldPort);
  285.                 SetPort(theWindow);
  286.                 ZoomWindow(theWindow, windowCode, FALSE);
  287.                 InvalRect(&theWindow->portRect);
  288.                 SetPort(oldPort);
  289.             }
  290.             
  291.             gWindowBounds[index] = (*(((WindowPeek)gTheWindow[index])->contRgn))->rgnBBox;
  292.             
  293.             break;
  294.     }
  295. }
  296.  
  297. void ShutDownEnvironment(void)
  298. {
  299.     ShutDownProgram();
  300.     ShutDownMSGGraphics();
  301.     ShutDownHelp();
  302.     CloseSounds();    
  303. }
  304.